package com.prj.ufdm.web.bpo._impl;

import com.prj.ufdm.web.bpo.SysFunctionBPO;
import com.prj.ufdm.web.constant.AuthUrlConstants;
import com.prj.ufdm.web.model.SysFunction;
import com.prj.ufdm.web.util.AuthRestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统功能管理
 */
@Service 
public class SysFunctionBPOImpl  implements SysFunctionBPO {
	
	@Autowired
	AuthRestTemplateUtil authRestTemplateUtil;
	
    /**
	 * 描述: 查询所有功能信息
	 * @auther 胡义振
	 * @param userId 用户ID
	 * @return 用户资源列表
	 * @throws Exception
	 */
    @Override
	public List<SysFunction> doGetFunctionList() throws Exception
	{
    	List<SysFunction> listSysFunction = authRestTemplateUtil.doAuthParamPostForList(AuthUrlConstants.GET_FUNCTION_LIST, null, null, SysFunction.class);
    	return listSysFunction;
	}

	@Override
	public Collection<GrantedAuthority> doGetGantedAuthoritiestByUserId(String userId) throws Exception {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("userId",userId);
		List<SysFunction> listSysFunction = authRestTemplateUtil.doAuthParamPostForList(AuthUrlConstants.GET_FUNCTION_LIST, paramMap, null, SysFunction.class);
		Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		for (SysFunction sysFunction : listSysFunction) {
			if (sysFunction != null && sysFunction.getFunctionUrl() != null) {
				// 此处将权限信息添加到 GrantedAuthority 对象中，在后面进行全权限验证时会使用 GrantedAuthority 对象
				if(sysFunction.getFunctionUrl()==null || sysFunction.getFunctionUrl().equals("")) {
					 grantedAuthorities.add(new SimpleGrantedAuthority(sysFunction.getId())); 
				}else {
				    grantedAuthorities.add(new SimpleGrantedAuthority(sysFunction.getFunctionUrl())); 
				}
			}
		}
		return grantedAuthorities;
	}
	
	
}
